package com.xie.springCloud.controller;

import com.xie.springCloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class ComsumerDeptController {

    //public static final String REST_URL_PREFIX = "http://localhost:8001";

    //ribbon 根据注册的服务名称 获取对应的ip 地址
    public static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";

    //夸服务用 基于HttpClient
    @Autowired private RestTemplate restTemplate;//远程调用模板
    // getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables)

    @RequestMapping("/consumer/dept/add")
    public boolean addDept(Dept dept){
        String url = REST_URL_PREFIX+"/dept/add";
        return restTemplate.postForObject(url,dept,boolean.class);
    }

    @RequestMapping("/consumer/dept/get/{id}")
    public Dept getById(@PathVariable("id") Long id){
        String url = REST_URL_PREFIX+"/dept/get/"+id;
        return restTemplate.getForObject(url, Dept.class);
    }

    @RequestMapping("/consumer/dept/list")
    public List<Dept> getList(){
        String url = REST_URL_PREFIX+"/dept/list";
        return restTemplate.getForObject(url, List.class);
    }

}
